<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib prefix="pg" uri="http://jsptags.com/tags/navigation/pager" %>

<!DOCTYPE html >
<html>
<head>



</head>
<body class="no-skin">

<div class="main-content">
		<!-- #section:basics/content.breadcrumbs -->
		<div class="breadcrumbs" id="breadcrumbs">
			<script type="text/javascript">
				try {
					ace.settings.check('breadcrumbs', 'fixed')
				} catch (e) {
				}
			</script>

			<ul class="breadcrumb">
				<li><i class="ace-icon fa fa-home home-icon"></i> <a href="#">首页</a>
				</li>
				<li class="active">教师菜单</li>
				<li class="active">考题管理</li>
			</ul>
			<!-- /.breadcrumb -->

			<!-- #section:basics/content.searchbox -->
			<div class="nav-search" id="nav-search">
				<form class="form-search">
					<span class="input-icon"> <input type="text"
						placeholder="Search ..." class="nav-search-input"
						id="nav-search-input" autocomplete="off" /> <i
						class="ace-icon fa fa-search nav-search-icon"></i>
					</span>
				</form>
			</div>
			<!-- /.nav-search -->

			<!-- /section:basics/content.searchbox -->
		</div>

		<!-- /section:basics/content.breadcrumbs -->
		<div class="page-content">
			<!-- #section:settings.box -->
			<div class="ace-settings-container" id="ace-settings-container">
				<div class="btn btn-app btn-xs btn-warning ace-settings-btn"
					id="ace-settings-btn">
					<i class="ace-icon fa fa-cog bigger-150"></i>
				</div>

				<div class="ace-settings-box clearfix" id="ace-settings-box">
					<div class="pull-left width-50">
						<!-- #section:settings.skins -->
						<div class="ace-settings-item">
							<div class="pull-left">
								<select id="skin-colorpicker" class="hide">
									<option data-skin="no-skin" value="#438EB9">#438EB9</option>
									<option data-skin="skin-1" value="#222A2D">#222A2D</option>
									<option data-skin="skin-2" value="#C6487E">#C6487E</option>
									<option data-skin="skin-3" value="#D0D0D0">#D0D0D0</option>
								</select>
							</div>
							<span>&nbsp; Choose Skin</span>
						</div>

						<!-- /section:settings.skins -->

						<!-- #section:settings.navbar -->
						<div class="ace-settings-item">
							<input type="checkbox" class="ace ace-checkbox-2"
								id="ace-settings-navbar" /> <label class="lbl"
								for="ace-settings-navbar"> Fixed Navbar</label>
						</div>

						<!-- /section:settings.navbar -->

						<!-- #section:settings.sidebar -->
						<div class="ace-settings-item">
							<input type="checkbox" class="ace ace-checkbox-2"
								id="ace-settings-sidebar" /> <label class="lbl"
								for="ace-settings-sidebar"> Fixed Sidebar</label>
						</div>

						<!-- /section:settings.sidebar -->

						<!-- #section:settings.breadcrumbs -->
						<div class="ace-settings-item">
							<input type="checkbox" class="ace ace-checkbox-2"
								id="ace-settings-breadcrumbs" /> <label class="lbl"
								for="ace-settings-breadcrumbs"> Fixed Breadcrumbs</label>
						</div>

						<!-- /section:settings.breadcrumbs -->

						<!-- #section:settings.rtl -->
						<div class="ace-settings-item">
							<input type="checkbox" class="ace ace-checkbox-2"
								id="ace-settings-rtl" /> <label class="lbl"
								for="ace-settings-rtl"> Right To Left (rtl)</label>
						</div>

						<!-- /section:settings.rtl -->

						<!-- #section:settings.container -->
						<div class="ace-settings-item">
							<input type="checkbox" class="ace ace-checkbox-2"
								id="ace-settings-add-container" /> <label class="lbl"
								for="ace-settings-add-container"> Inside <b>.container</b>
							</label>
						</div>

						<!-- /section:settings.container -->
					</div>
					<!-- /.pull-left -->

					<div class="pull-left width-50">
						<!-- #section:basics/sidebar.options -->
						<div class="ace-settings-item">
							<input type="checkbox" class="ace ace-checkbox-2"
								id="ace-settings-hover" /> <label class="lbl"
								for="ace-settings-hover"> Submenu on Hover</label>
						</div>

						<div class="ace-settings-item">
							<input type="checkbox" class="ace ace-checkbox-2"
								id="ace-settings-compact" /> <label class="lbl"
								for="ace-settings-compact"> Compact Sidebar</label>
						</div>

						<div class="ace-settings-item">
							<input type="checkbox" class="ace ace-checkbox-2"
								id="ace-settings-highlight" /> <label class="lbl"
								for="ace-settings-highlight"> Alt. Active Item</label>
						</div>

						<!-- /section:basics/sidebar.options -->
					</div>
					<!-- /.pull-left -->
				</div>
				<!-- /.ace-settings-box -->
			</div>
			<!-- /.ace-settings-container -->

			<!-- /section:settings.box -->
			<div class="page-content-area">
				
				<div class="row">
					<div class="col-xs-12">
						<!-- PAGE CONTENT BEGINS -->
						<div class="row">
							<div class="col-xs-12"></div>
							<!-- /.span -->
						</div>

						<!-- /.row -->

						<div class="hr hr-10 dotted hr-double"></div>
  
						<!-- 查看详细试题模态框 -->
						<div class="modal fade" id="showTestModal" tabindex="-1"
							role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
							<div class="modal-dialog">
								<div class="modal-content">
									<div class="modal-header">
										<button type="button" class="close" data-dismiss="modal"
											aria-hidden="true">&times;</button>
										<h4 class="modal-title" id="myModalLabel">查看详细试题</h4>
									</div>
									<div class="modal-body">
										<label>试题科目：&nbsp;&nbsp;</label><span class="sub-name"></span>&nbsp;&nbsp;（<span
											class="test-type"></span>每题<span class="test-score"></span>分)<br />
										<label>题目：&nbsp;&nbsp;</label><span class="content"></span><br />
										<label>选项A：&nbsp;&nbsp;</label><span class="choose-a"></span><br />
										<label>选项B：&nbsp;&nbsp;</label><span class="choose-b"></span><br />
										<label>选项C：&nbsp;&nbsp;</label><span class="choose-c"></span><br />
										<label>选项D：&nbsp;&nbsp;</label><span class="choose-d"></span><br />
										<label style="font-weight: bold;">答案：&nbsp;&nbsp;</label><span
											class="answer" style="font-weight: bold;"></span><br />
									</div>
									<div class="modal-footer">
										<button type="button" class="btn btn-default"
											data-dismiss="modal">关闭</button>
									</div>
								</div>
								<!-- /.modal-content -->
							</div>
							<!-- /.modal -->
						</div>
						<!-- 添加试题模态框 -->
						<div class="modal fade" id="showAddTestModal" tabindex="-1"
							role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
							<div class="modal-dialog">
								<div class="modal-content">
									<div class="modal-header">
										<button type="button" class="close" data-dismiss="modal"
											aria-hidden="true">&times;</button>
										<h4 class="modal-title" id="myModalLabel">添加试题</h4>
									</div>
									<form id="testForm" method="post">
										<div class="modal-body">
											<p>
												<label>试题科目：&nbsp;&nbsp;</label> 
												<select class="show-subject" name="subId" style="width: 150px;"></select>
											</p>
											<p>
												<label>试题类型：&nbsp;&nbsp;</label> 
												<select class="show-testType" name="typeId" style="width: 100px;"></select>&nbsp;
												<label>分值：</label><span class="score">0</span>&nbsp;分
											</p>
											<p>
												<label>题&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;目：&nbsp;&nbsp;</label>
												<textarea type="text" class="content" name="content" placeholder="&nbsp;&nbsp;请填写题目内容"
													cols="50" rows="9" style="resize:none;overflow-x:hidden;"></textarea>
											</p>
											<p> 
												<label>选&nbsp;&nbsp;项&nbsp;&nbsp;&nbsp;A：&nbsp;&nbsp;</label><input type="text"
													name="chooseA" class="chooseA" placeholder="&nbsp;&nbsp;请填写选项A内容" style="width:300px;">
											</p>
											<p>
												<label>选&nbsp;&nbsp;项&nbsp;&nbsp;&nbsp;B：&nbsp;&nbsp;</label><input type="text"
													name="chooseB" class="chooseB" placeholder="&nbsp;&nbsp;请填写选项B内容" style="width:300px;">
											</p>
											<p>
												<label>选&nbsp;&nbsp;项&nbsp;&nbsp;&nbsp;C：&nbsp;&nbsp;</label><input type="text"
													name="chooseC" class="chooseC" placeholder="&nbsp;&nbsp;请填写选项C内容" style="width:300px;">
											</p>
											<p>
												<label>选&nbsp;&nbsp;项&nbsp;&nbsp;&nbsp;D：&nbsp;&nbsp;</label><input type="text"
													name="chooseD" class="chooseD" placeholder="&nbsp;&nbsp;请填写选项D内容" style="width:300px;">
											</p>
											<p>
												<label style="font-weight: bold;">答&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;案：&nbsp;&nbsp;</label> 
												<span class="answerTotal" style="font-weight: bold;"> 
													<label class="radio-inline checkbox-inline awA"> 
														<input type="radio" name="answer" value="A" /> A
													</label> 
													<label class="radio-inline checkbox-inline awB">
														<input type="radio" name="answer" value="B" /> B
													</label> 
													<label class="radio-inline checkbox-inline awC"> 
														<input type="radio" name="answer" value="C" /> C
													</label>
													<label class="radio-inline checkbox-inline awD"> 
														<input type="radio" name="answer" value="D" /> D
													</label>
												</span>
											</p>
										</div>
										<div class="modal-footer">
										<button type="button" class="btn btn-info saveType"
												data-dismiss="modal">提交</button>
										<button type="button" class="btn btn-default"
												data-dismiss="modal">关闭</button>
										</div>
									</form>
								</div>
								<!-- /.modal-content -->
							</div>
							<!-- /.modal -->
						</div>
						<!-- 修改试题模态框 -->
						<div class="modal fade" id="showEdtiTestModal" tabindex="-1"
							role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
							<div class="modal-dialog">
								<div class="modal-content">
									<div class="modal-header">
										<button type="button" class="close" data-dismiss="modal"
											aria-hidden="true">&times;</button>
										<h4 class="modal-title" id="myModalLabel">修改试题</h4>
									</div>
									<form id="editTestForm" method="post">
										<div class="modal-body">
											<p>
												<label>试题科目：&nbsp;&nbsp;</label> 
												<select class="show-subject" name="subId" style="width: 150px;"></select>
											</p>
											<p>
												<label>试题类型：&nbsp;&nbsp;</label> 
												<select class="show-testType" name="typeId" style="width: 100px;"></select>&nbsp;
												<label>分值：</label><span class="score">0</span>&nbsp;分
											</p>
											<p>
												<label>题&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;目：&nbsp;&nbsp;</label>
												<textarea type="text" class="content" name="content" placeholder="&nbsp;&nbsp;请填写题目内容"
													cols="50" rows="9" style="resize:none;overflow-x:hidden;"></textarea>
											</p>
											<p> 
												<label>选&nbsp;&nbsp;项&nbsp;&nbsp;&nbsp;A：&nbsp;&nbsp;</label><input type="text"
													name="chooseA" class="chooseA" placeholder="&nbsp;&nbsp;请填写选项A内容" style="width:300px;">
											</p>
											<p>
												<label>选&nbsp;&nbsp;项&nbsp;&nbsp;&nbsp;B：&nbsp;&nbsp;</label><input type="text"
													name="chooseB" class="chooseB" placeholder="&nbsp;&nbsp;请填写选项B内容" style="width:300px;">
											</p>
											<p>
												<label>选&nbsp;&nbsp;项&nbsp;&nbsp;&nbsp;C：&nbsp;&nbsp;</label><input type="text"
													name="chooseC" class="chooseC" placeholder="&nbsp;&nbsp;请填写选项C内容" style="width:300px;">
											</p>
											<p>
												<label>选&nbsp;&nbsp;项&nbsp;&nbsp;&nbsp;D：&nbsp;&nbsp;</label><input type="text"
													name="chooseD" class="chooseD" placeholder="&nbsp;&nbsp;请填写选项D内容" style="width:300px;">
											</p>
											<p>
												<label style="font-weight: bold;">答&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;案：&nbsp;&nbsp;</label> 
												<span class="answerTotal" style="font-weight: bold;"> 
													<label class="radio-inline checkbox-inline awA"> 
														<input type="radio" name="answer" value="A" /> A
													</label> 
													<label class="radio-inline checkbox-inline awB">
														<input type="radio" name="answer" value="B" /> B
													</label> 
													<label class="radio-inline checkbox-inline awC"> 
														<input type="radio" name="answer" value="C" /> C
													</label>
													<label class="radio-inline checkbox-inline awD"> 
														<input type="radio" name="answer" value="D" /> D
													</label>
												</span>
											</p>
										</div>
										<div class="modal-footer">
										<button type="button" class="btn btn-info saveType"
												data-dismiss="modal">提交</button>
										<button type="button" class="btn btn-default"
												data-dismiss="modal">关闭</button>
										</div>
									</form>
								</div>
								<!-- /.modal-content -->
							</div>
							<!-- /.modal -->
						</div>
						
						<!-- .testList -->
						<div id="testList">
							<div id="messageDiv">
								<c:if test="${not empty message}">
									<div class="alert alert-success alert-dismissible" role="alert">
										<button type="button" class="close" data-dismiss="alert"
											aria-label="Close">
											<span aria-hidden="true">&times;</span>
										</button>
										<strong class="center">${message}!</strong>
									</div>
								</c:if>
							</div>

							<table id="sample-table-1"
								class="table table-striped table-bordered table-hover">
								<thead>
									<a title="添加试题" class="btn btn-primary btn-xs show-addtest">添加试题</a>
									<tr>
										<th>编号</th>
										<th>科目</th>
										<th>题干</th>
										<th>A</th>
										<th>B</th>
										<th>C</th>
										<th>D</th>
										<th>答案</th>
										<th>题目类型</th>
										<th>题目分数</th>
										<th class="hidden-480"><spring:message
												code="page.action.message" /></th>
									</tr>
								</thead>
								<tbody>
									<c:forEach items="${testVOList.datas}" var="testVO">
										<tr>
											<td>${testVO.id}</td>
											<td>${testVO.subject.name}</td>
											<td><div class="content">${testVO.content}</div></td>
											<td><div class="choose">${testVO.chooseA}</div></td>
											<td><div class="choose">${testVO.chooseB}</div></td>
											<td><div class="choose">${testVO.chooseC}</div></td>
											<td><div class="choose">${testVO.chooseD}</div></td>
											<td>${testVO.answer}</td>
											<td>${testVO.examTestType.testType}</td>
											<td>${testVO.examTestType.testTypeScore}</td>
											<td>
												<a title="查看试题" class="btn btn-success btn-xs show-test"><i class="glyphicon glyphicon-eye-open"></i></a>
												<a title="修改试题" class="btn btn-warning btn-xs edit-test"><i class="glyphicon glyphicon-pencil"></i></a>
												<a title="删除试题" class="btn btn-danger btn-xs delete-test"><i class="glyphicon glyphicon-trash"></i></a>
											</td>
										</tr>
									</c:forEach>
								</tbody>
							</table>
						</div>
						<!-- /.userList -->
						<div class="page" align="center">
							<jsp:include page="../public/page.jsp">
								<jsp:param value="tests" name="url"/>
								<jsp:param value="${testVOList.totalSize }" name="items"/>
								<jsp:param value="${testVOList.totalSize }" name="subId"/>
							</jsp:include>
						</div>
						<!-- PAGE CONTENT ENDS -->
					</div>
					<!-- /.col -->
				</div>
				<!-- /.row -->
			</div>
			<!-- /.page-content-area -->
		</div>
		<!-- /.page-content -->
	</div>



	<script type="text/javascript">
		$('.show-test').click(function() {
			var testId = $(this).parent().siblings().eq(0).text();
			$.ajax({
				type : 'POST',
				url : '${pageContext.request.contextPath}/test/getTestById',
				data : {
					testId : testId
				},
				dataType : "json",
				success : function(data) {
					$('#showTestModal .sub-name').text(data.subject.name);
					$('#showTestModal .test-type').text(data.testType);
					$('#showTestModal .test-score').text(data.testScore);
					$('#showTestModal .content').text(data.content);
					$('#showTestModal .choose-a').text(data.chooseA);
					$('#showTestModal .choose-b').text(data.chooseB);
					$('#showTestModal .choose-c').text(data.chooseC);
					$('#showTestModal .choose-d').text(data.chooseD);
					$('#showTestModal .answer').text(data.answer);
				}
			});
			$('#showTestModal').modal('show');
		});
		
		function subList(value){
			$.ajax({
				type : 'POST',
				url : '${pageContext.request.contextPath}/subject/showSubjects',
				dataType : 'json',
				success : function(data) {
					var html = '<option value="0">请选择</option>';
					for (var i = 0; i < data.length; i++) {
							html += '<option value="'
								+ (data[i].id) + '" >'
								+ (data[i].name)
								+ '</option>';
						
					}
					$('.show-subject option').remove();
					$('.show-subject').append(html);
					$('.show-subject').find("option[value="+value+"]").attr("selected",true);
				}
			});
		}
		
		function typeList(value){
			$.ajax({
				type : 'POST',
				url : '${pageContext.request.contextPath}/test/getTypes',
				dataType : 'json',
				success : function(data) {
					var html = '<option value="0">请选择</option>';
					for (var j = 0; j < data.length; j += 3) {
						html += '<option value="'
								+ (data[j]) + '">'
								+ (data[j + 1])
								+ '</option>';
					}
					$('.show-testType option').remove();
					$('.show-testType').append(html);
					$('.show-testType').find("option[value="+value+"]").attr("selected",true);
				}
			});
		};
		
		function getScore(typeId){
				if (typeId != 0) {
					$.ajax({
						type : 'POST',
						url : '${pageContext.request.contextPath}/test/getType',
						data : {
							typeId : typeId
						},
						dataType : 'json',
						success : function(data) {
							$('.score').text(data.testTypeScore);							
							inputType(typeId); 
						}
					});
				}else{
					$('.score').text(0);
				}
		}
		
		function inputType(typeId){
			if(typeId==1){
				$('.answerTotal input[name="answer"]').attr('type','radio');
			}else{
				$('.answerTotal input[name="answer"]').attr('type','checkbox');
			}
			
		}
		
		$('.show-testType').change(function(){
			getScore($(this).val());
		})
		
		$('.show-addtest').click(function() {
			subList(0);
			typeList(0);
			getScore(0);
			$('#showAddTestModal').modal('show');
			saveTest('add','showAddTestModal',null);
			
		});
		
		$('.edit-test').click(function() {
			var testId = $(this).parent().siblings().eq(0).text();
			$.ajax({
				type:'POST',
				url : '${pageContext.request.contextPath}/test/getTestById',
				data:{testId:testId},
				dataType:'json',
				success:function(data){
					subList(data.subID);
					typeList(data.testTypeId);
					getScore(data.testTypeId);
					$('#editTestForm .content').text(data.content);				
					$('#editTestForm .chooseA').val(data.chooseA);
					$('#editTestForm .chooseB').val(data.chooseB);
					$('#editTestForm .chooseC').val(data.chooseC);
					$('#editTestForm .chooseD').val(data.chooseD);
					$('.answerTotal label').remove();
						var coarr=new Array('A','B','C','D');
						var html='';
						if(data.testTpyeId==1){
							for(var i=0;i<4;i++){
								html+='<label class="radio-inline checkbox-inline"><input type="radio" name="answer" value="'+coarr[i]+'" />'+coarr[i]+'</label>'
							}
						}else{
							for(var i=0;i<4;i++){
								html+='<label class="radio-inline checkbox-inline"><input type="checkbox" name="answer" value="'+coarr[i]+'" />'+coarr[i]+'</label>'
							}
						} 
						$('.answerTotal').append(html); 
						var arr=(data.answer).split('');
						for(var i in arr){
							$('#editTestForm input[value="'+arr[i]+'"]').attr('checked',true);
						}		
				}
				
			});
			$('#showEdtiTestModal').modal('show');
			saveTest('edit','showEdtiTestModal',testId);
		}); 
		
		function saveTest(url,did,id){
			$('#'+did+' .saveType').click(function(){
				//if($('#addTestForm').vaild()){	//前端校验
					var subId=$('#'+did+' select[name="subId"]').val();
					var typeId=$('#'+did+' select[name="typeId"]').val();
					var content=$('#'+did+' textarea[name="content"]').val();
					var chooseA=$('#'+did+' input[name="chooseA"]').val();
					var chooseB=$('#'+did+' input[name="chooseB"]').val();
					var chooseC=$('#'+did+' input[name="chooseC"]').val();
					var chooseD=$('#'+did+' input[name="chooseD"]').val();
					var answer='';
					if(typeId==1){
						answer+=$('#'+did+' input[name="answer"]:radio:checked').val();
					}else if(typeId==2){
						var arr=$('#'+did+' input[name="answer"]:checkbox:checked');
						$.each(arr,function(index,item){
							answer+=(index == 0 ? $(item).val():(' 、'+$(item).val()));
						})
					}else{
						alert('请选择考题类型！');
					}
					
	 				$.ajax({
						type:'POST',
						url:'${pageContext.request.contextPath}/test/'+url,
						data:{
							id:id,
							subID:subId,
							'examTestType.id':typeId,
							testTypeId:typeId,
							content:content,
							chooseA:chooseA,
							chooseB:chooseB,
							chooseC:chooseC,
							chooseD:chooseD,
							answer:answer
						},
						dataType:'json',
						success:function(data){
							//判断是否成功
		 					if(data.success){
		 						if(id==null){
		 							alert('添加成功！');
		 						}else{
		 							alert('修改成功！');
		 						}
								
								window.location.reload();
							}else{
							 	var messageMap=data.messageMap;
								for(var key in messageMap){
									var value=messageMap[key];
									$('#'+did+' input[name="'+key+'"]').after('<label class="jsr303-message"></label>');
								}
								alert(sc+'失败！-' +messageMap['message']); 
							} 
						}			
					});	 
				//}		
			});
		}
		
		$('.delete-test').click(function(){
			var testId = $(this).parent().siblings().eq(0).text();
			var con=confirm("是否删除？")
			if(con==true){
				$.ajax({
					type:'POST',
					url:'${pageContext.request.contextPath}/test/delete',
					data:{testId:testId},
					dataType:'json',
					success:function(data){
						if(data.success){
							window.location.reload();
							alert('删除成功！');	
						}else{
							alert('删除失败！'); 
						}
					}
				});
			}
		});
	</script>
</html>
</body>
</html>